$(function() {
	// 定义变量
	var staticSpeed = 1;
	//全局变量
	//经世优学

	// 学习通变量start
	//查找当前播放的位置
	var index = 0;
	$(".ncells h4").each((i, item) => {
		if ($(item).hasClass("currents")) {
			index = i;
			return false;
		}
	});
	// 学习通变量end
	//默认设置cookie，防止报错
	//创建一个div
	var div = document.createElement('div');
	// 设置样式
	div.innerHTML =
		'<div id="box" class="box" style="border-radius: 0 0 5px 5px  ;z-index: 9999;position:fixed;top: 0;padding: 10px;opacity: 0.9;background-color:#7d7d7d;color:white!important;"><h2>按Home键&nbsp;隐藏/显示</h2>			<div>				<fieldset style="padding: 10px;"><legend>美和易斯课堂</legend>					<div class="grid">						<div>倍速<input id="mhysSpeed"  onkeydown="return false;" step="0.1" min="1" max="8" type="number" value="1" placeholder="输入" style="margin-right: 10px;"/><button style="padding: 0 3px;background-color: white;border: 0;" id="mhysSure" class="btn" href="#" id="btnSure">确定</button></div>					</div><div ><input id="mhysAutoPlay" style="vertical-align: text-top;" checked="checked"  type="checkbox" value="" ><text>自动播放</text> <input id="mute" style="vertical-align: text-top;" checked="checked"  type="checkbox" value="" ><text>静音</text></div>				</fieldset>				<fieldset style="padding: 10px;">					<legend>知到</legend>	<div><input id="zdAutoPlay" style="vertical-align: text-top;" type="checkbox" checked="checked" value="">自动播放<input id="zdMute" style="vertical-align:text-top;" type="checkbox" checked="checked" value="">静音<input id="zdDoubleSpeed" style="vertical-align:text-top;" checked="checked" type="checkbox" value="">最高倍速</div>				</fieldset>				<fieldset style="padding: 10px;">					<legend>学习通</legend>					<div><input id="xxtAutoPlay" style="vertical-align: text-top;" checked="checked" type="checkbox" value="">自动播放<input id="xxtMute" style="vertical-align: text-top;" checked="checked" type="checkbox" value="">静音<input id="xxtDoubleSpeed" checked="checked" style="vertical-align: text-top;" type="checkbox" value="">最高倍速</div>				</fieldset>				<fieldset style="padding: 10px;">					<legend>关于插件</legend>					<div>						<div>版本：2.2.4</div>						<div>作者：小星博博</div>	<div>邮箱：168670535@qq.com</div></div></fieldset></div></div>'; //设置显示的数据，可以是标签．
	div.className = ''; //设置div的属性，如：class，title，id 等等
	var bo = document.body; //获取body对象.
	//动态插入到body中
	bo.insertBefore(div, bo.lastChild);
	//隐藏、显示
	$(document).keydown(function(event) {
		if (event.keyCode == 36) {
			if ($("#box").is(":hidden")) {
				$("#box").show();
			} else {
				$("#box").hide();
			}
		}
	});
	//静音
	//初始化倍速的cookie
	if ($.cookie("speed") == null) {
		$.cookie('speed', '1');
	} else {
		var s = $.cookie('speed');
		$("#mhysSpeed").val(parseInt(s))
	}
	// 设置美和易斯倍速
	$("#mhysSure").on("click", function() {
		var speed = $("#mhysSpeed").val();
		$.cookie('speed', null);
		$.cookie('speed', speed);
		console.log($.cookie('speed'))
	})


	//定时器
	var interval = setInterval(function() {

		var elementPlayView = document.querySelector('.pv-video');
		if (elementPlayView) {
			mhys();
		}
		var objZd = $("#vjs_container_html5_api");
		if (objZd) {
			//调用知到
			zd()
		}
		// 学习通
		if ($("#xxtAutoPlay").is(":checked")) {
			xxt();
		}
	}, 2000);

	function mhys() {
		//读取cookic
		var cookicSpeed = cookicSpeed = $.cookie("speed");
		// 判断是否是播放界面
		// 解除视频自动暂停
		if (!-[1, ] && !window.XMLHttpRequest || navigator.userAgent.indexOf("MSIE 8.0") > 0) {
			document.onfocusout = function() {
				return true;
			}
		} else {
			window.onblur = function() {
				return true;
			}
		}
		// 播放按钮
		if($("#mhysAutoPlay").is(":checked")){
			var elementBtnPlay = document.querySelector('.pv-controls-left .pv-icon-btn-play')
					if (elementBtnPlay) {
						elementBtnPlay.click();
					}
		}
		//倍速播放
		var elementVideo = document.querySelector("video");
		if (elementVideo != null) {
			if (staticSpeed != cookicSpeed) {
				elementVideo.playbackRate = parseInt(cookicSpeed)
				staticSpeed = cookicSpeed;
			}
		}
		// 判断是否播放完毕
		if (document.querySelector(".pv-progress-current-bg").style.width == "100%") {
			//延迟执行，防止网卡，直接跳过
			setTimeout(function() {
				var elmentLi = document.querySelector(".vedio-play-conts-left-chapter-list .active");
				if (elmentLi.nextElementSibling == null) {
					var elementLastDirectory = elmentLi.parentNode.parentNode.parentNode.nextElementSibling;
					if (elementLastDirectory != null) {
						elementLastDirectory.querySelectorAll(".vedio-play-conts-left-chapter-list li")[0].click();
					} else {
						//关闭定时器
						clearInterval(interval)
						alert('本课程已播放完成');
					}

				} else {
					elmentLi.nextElementSibling.click()
				}
			}, 2000)
		}
		//静音
		var elementVolumeon = document.querySelector(".pv-icon-volumeon");
		if (elementVolumeon) {
			if ($("#mute").is(':checked')) {
				elementVolumeon.click();
			}

		}
	}
	//知到
	function zd() {
		//判断是否值智慧树
		var zhsVideo = $("#container>.video-js>.videoArea");
		if (!zhsVideo.length > 0) {
			return false;
		}
		//自动播放
		if ($("#zdAutoPlay").is(":checked")) {
			var pauseButtonObj = document.getElementsByClassName("pauseButton");
			if (pauseButtonObj[0] == undefined) {
				document.getElementById("playButton").click();
			}
		}

		//静音
		if ($("#zdMute").is(":checked")) {
			var volumeBoxObj = document.getElementsByClassName('volumeBox');
			if (!volumeBoxObj[0].classList.contains("volumeNone")) {
				//获取子元素
				var volumeBoxObjFirstChild = volumeBoxObj[0].firstChild;
				volumeBoxObjFirstChild.click();
				console.log("脚本提示：静音执行成功")
			}
		}

		//流畅画质
		var definiBoxObj = document.getElementsByClassName("definiBox");
		if (definiBoxObj[0]) {
			var definiBoxObjFirtNode = definiBoxObj[0].firstChild;
			if (definiBoxObjFirtNode.innerHTML != "流畅") {
				//流畅画质单击
				var line1bqObj = document.getElementsByClassName('line1bq');
				line1bqObj[0].click()
				console.log("脚本提示：流畅画质开启成功")
			}
		}

		//1.5倍速
		if ($("#zdDoubleSpeed").is(":checked")) {
			var speedBoxObj = document.getElementsByClassName("speedBox");
			if (speedBoxObj[0]) {
				var speedBoxObjFirtNode = speedBoxObj[0].firstChild;
				if (speedBoxObjFirtNode.innerHTML != "X 1.5") {
					//1.5倍速单击
					var speedObj = document.getElementsByClassName('speedTab15');
					speedObj[0].click()
					console.log("脚本提示：1.5倍速开启成功")
				}
			}
		}
		//遍历所有要播放对象
		var videoObj = document.querySelectorAll(".video .cataloguediv-c");
		for (var j = 0; j < videoObj.length; j++) {
			console.log(j + "---" + videoObj[j].nodeName)
			if (videoObj[j].querySelector(".time_icofinish")) {
				console.log(j + "已完成")
			} else {
				//判断是否包含选中的类名
				if (videoObj[j].parentElement.getAttribute('class').indexOf('current_play') > -1) {
					console.log("脚本提示：正在播放")
					//结束循环
					break;
				} else {
					videoObj[j].parentNode.click()
					//结束循环
					break;
				}

			}
		}

		//获取弹出框
		var windowObj = document.getElementsByClassName("dialog-test");
		if (windowObj[0] != undefined) {
			//获取答案选项
			var liObj = document.getElementsByClassName('topic-item');
			//通过选项的个数，产生随机数
			var rad = Math.floor(Math.random() * liObj.length)
			//选择随机项
			liObj[rad].click();
			console.log("脚本提示：答案已选择");
			//关闭弹窗
			document.getElementsByClassName('btn')[6].click();
			console.log("脚本提示：弹题窗口已关闭")
			//播放按钮单击
			var videoObj = document.getElementById('playButton');
			videoObj.click();
			console.log("脚本提示：开始自动播放")
		}

	}

	function xxt() {
		//获取iframe
		var video = $("iframe").contents().find("iframe").contents();
		//播放函数
		var play = function() {
			video.find("#video > button").click();
			var jy = video.find(
				"#video > div.vjs-control-bar > div.vjs-volume-panel.vjs-control.vjs-volume-panel-vertical > button");
			//静音
			if ($("#xxtMute").is(":checked")) {
				if (jy.attr("title") != "取消静音") {
					jy.click();
				}
			}
			//倍速
			if ($("#xxtDoubleSpeed").is(":checked")) {
				//获取video.js控件
				var videoSpeed = video.find("#video_html5_api")[0];
				//设置播放速率=2
				videoSpeed.playbackRate = 2;
				/* var elementNowSpeed =$(".vjs-playback-rate-value");
				if (elementNowSpeed) {
					if ($(".vjs-playback-rate-value").innerText!= "2x") {
						$(".vjs-menu .vjs-menu-item").click()
					}
				} */

			}
		}
		//如果正在加载
		var load = video.find("#loading");
		if (load.css("visibility") != "hidden") {
			return false;
		}
		//获取当前进度
		var spans = video.find("#video > div.vjs-control-bar > div.vjs-progress-control.vjs-control > div").attr(
			"aria-valuenow");
		// 如果还没播放完
		if (spans != 100) {
			play();
		}
		//如果播放结束
		if (spans == 100) {
			console.log("第" + (index + 1) + "章节结束");
			// 自动跳到下一个视频
			index++;
			window.location.href = document.querySelectorAll(".ncells")[index].querySelector("a").href;
			$("span[title='视频']").trigger("click");

			var bs = false;
			$(".onetoone").find(".flush").each(function() {
				if (bs) {
					$(this).prev("a").on('click', "#coursetree>ncells", function() {
						console.log("已结束章节：" + $(this).prev("a").attr("title"));
					});
					var str = $(this).prev("a").attr("href");
					str = str.match(/'(\S*)'/)[1];
					var reg = new RegExp("'", "g");
					str = str.replace(reg, "");
					var href = str.split(",");
					getTeacherAjax(href[0], href[1], href[2])
					bs = false;
				}
				if ($(this).css("display") == "block") {
					bs = true;
				}
			})
		}

	}
})
var jsyxNowPlay=0;	
setInterval(function(){
	//$(".cursorsHover")[1].click()
	var progressDom= $(".vjs-progress-holder,.vjs-slider,.vjs-slider-horizontal>.vjs-play-progress.vjs-slider-bar");
	if(progressDom.length==3){
		if(progressDom[2].style.width=="100%"){
			jsyxNowPlay++;
			if(jsyxNowPlay<=$("h5").length){
				$("h5")[jsyxNowPlay].click()
			}
		}
			
	}
},2000)




